/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaRoleResRel;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class RoleResourceRelDao {

	public static void createRoleResRel(SoaRoleResRel user)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();

			DaoManager.getDao().save(user);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(RoleResourceRelDao.class);
			logger.error("RoleServiceRelDao.createRoleResRel()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createRoleResRel() error", e);
		} finally {
			tx.end();
		}

	}

	public static void createRoleResRel(List<SoaRoleResRel> r)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();

			DaoManager.getDao().saveOrUpdateAll(r);
			DaoManager.getDao().flush();
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(RoleResourceRelDao.class);
			logger.error("RoleServiceRelDao.createRoleResRel(List)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createRoleResRel(List) error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeRoleResRel(SoaRoleResRel r) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().delete(r);
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(RoleResourceRelDao.class);
			logger.error("RoleServiceRelDao.removeRoleResRel()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeRoleResRel() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeRoleResRel(List<SoaRoleResRel> r) throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().deleteAll(r);
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(RoleResourceRelDao.class);
			logger.error("RoleServiceRelDao.removeRoleResRel(List)", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeRoleResRel(List) error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeRoleResRelByRoleId(String id)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaRoleResRel r where r.compId.roleId = ?";
			List o = DaoManager.getDao().find(hql, id);
			DaoManager.getDao().deleteAll(o);
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(RoleResourceRelDao.class);
			logger.error("RoleServiceRelDao.removeRoleResRelByRoleId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeRoleResRelByRoleId() error", e);
		} finally {
			tx.end();
		}

	}

	public static void removeRoleResRelByResId(String id)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaRoleResRel r where r.compId.resId = ?";
			List o = DaoManager.getDao().find(hql, id);
			DaoManager.getDao().deleteAll(o);
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(RoleResourceRelDao.class);
			logger.error("RoleServiceRelDao.removeRoleResRelByResId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeRoleResRelByResId() error", e);
		} finally {
			tx.end();
		}

	}
}
